package com.wits.av;

import android.util.Log;

/**
 * Created by zhengboyuan on 2018-03-12.
 */

public class JniPsCaster {

    public static final int 	PSCASTER_PROTOCOL_UDP = 1;
    public static final int 	PSCASTER_PROTOCOL_TCP = 2;
    public static final int 	PSCASTER_PROTOCOL_RTP_UDP = 3;	/// 基于UDP的RTP传输.
    public static final int 	PSCASTER_PROTOCOL_RTP_TCP = 4;

    public static final int    INVALID_HANDLE = -1;


    public static final int 	PSCASTER_EVENT_CONNECTING = 0x11;   /// 连接中.
    public static final int 	PSCASTER_EVENT_CONNECTED = 0x12;    /// 连接成功.
    public static final int 	PSCASTER_EVENT_DISCONNECTED = 0x13; /// 断开连接了.


    static {
        try {
            Log.i("jni", "loading JniPsCaster library");
            System.loadLibrary("PsCaster");
            Log.i("jni", "load JniPsCaster done");
        } catch (UnsatisfiedLinkError e) {
            Log.e("jni", "failed to load JniPsCaster");
        }
    }


    /**
     * 初始化
     * @return 0 表示成功
     */
    public native static int pscaster_init();

    /**
     * 反初始化
     * @return
     */
    public native static int pscaster_quit();

    /**
     * 打开通道
     * @param protocol	协议
     * @param outPort   本地端口, 0 表示自动生成
     * @param fmt   媒体格式
     * @return 通道句柄, 0 表示失败
     */
    public native static int pscaster_open(int protocol, int outPort, MFormat fmt);

    /**
     * 关闭通道
     * @param handle 通道句柄
     */
    public native static void pscaster_close(int handle);

    /**
     * 添加发送目标地址
     * @param handle
     * @param ip        IP地址
     * @param port      端口
     * @return
     */
    public native static int pscaster_add_target(int handle, String ip, int port);

    /**
     * 删除发送目标地址
     * @param handle    通道句柄
     * @param ip        IP地址
     * @param port      端口
     * @return
     */
    public native static int pscaster_remove_target(int handle, String ip, int port);

    /**
     * 删除所有发送目标
     * @param handle    通道句柄
     * @return
     */
    public native static int pscaster_remove_all_target(int handle);


    /**
     * 写视频包
     * @param handle  通道句柄
     * @param pkt  包
     * @return 0 表示成功
     */
    public native static int pscaster_write(int handle, MPacket pkt);


    /**
     * 设置socket发送缓冲区大小
     * 在 open 之前调用才会生效
     * @param size
     */
    public native static void pscaster_setSendBufSize(int size);


    /**
     * 打开通道
     * @param protocol
     * @param outPort   本地端口, 0 表示自动生成
     * @param fmt
     * @param payload   RTP payload
     * @param ssrc      RTP SSRC
     * @param es    > 0 表示使用RTP传输ES流
     * @return
     */
    public native static int pscaster_openEx(int protocol, int outPort, MFormat fmt, int payload, int ssrc, int es);

    /**
     * 设置事件回调
     * @param handle
     * @param listener
     * @return
     */
    public native static int pscaster_setEventCallback(int handle, PsCasterListener listener);

    /**
     * 启用/禁止重连
     * @param enabled
     */
    public native static void pscaster_enableReconnect(boolean enabled);

    /**
     * 设置RTP over TCP时, 前缀长度. 默认为2字节
     * @param size
     */
    public native static void pscaster_setTcpPrefixSize(int size);

}
